package com.meichis.yndms.adapter.RecycleViewItemType;

import android.app.Activity;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meichis.mcsappframework.adapter.recyclerview.base.ItemViewDelegate;
import com.meichis.mcsappframework.adapter.recyclerview.base.ViewHolder;
import com.meichis.mcsappframework.utils.DateUtil;
import com.meichis.mcsappframework.utils.ParseUtil;
import com.meichis.mcsappframework.utils.SharePreferenceUtil;
import com.meichis.yndms.R;
import com.meichis.yndms.common.CameraView;
import com.meichis.yndms.common.PictureUtils;
import com.meichis.yndms.model.InspectTemplate_Item;
import com.meichis.yndms.model.Picture;
import com.meichis.yndms.model.UserInfo;
import com.meichis.yndms.ui.activity.VST_FreeQuestionnaireLogicActivity;
import com.meichis.yndms.ui.view.IPictureView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author szw
 * @version 1.0
 * @date 2020/11/12 9:27
 */
public class FillMode_Picture implements ItemViewDelegate<InspectTemplate_Item> {

    private final Activity activity;
    private List<InspectTemplate_Item> items;
    private String staffCode;

    public FillMode_Picture(Activity activity, List<InspectTemplate_Item> items) {
        staffCode = ((UserInfo) SharePreferenceUtil.getInstance().getObjectValue(SharePreferenceUtil.PREFERENCES_USERINFO)).getUsername();
        this.items = items;
        this.activity = activity;

    }

    @Override
    public int getItemViewLayoutId() {
        return R.layout.fill_mode_picture;
    }

    @Override
    public boolean isForViewType(InspectTemplate_Item item, int position) {
        return item.getItem().getFillMode() == 9;
    }

    @Override
    public void convert(final ViewHolder holder, final InspectTemplate_Item item, final int position) {
        TextView tv_group = holder.getView(R.id.tv_group);

        final CameraView cameraView = holder.getView(R.id.fm_cameraView);

        cameraView.setWaterParams("拍摄时间：" + DateUtil.getCurDateStr(DateUtil.FORMAT_YMDHMS) +
                "\n员工编码：" + staffCode);
        cameraView.setPictureMaxCount(ParseUtil.parseInt(item.getItem().getMaxValue()) == 0 ? 6 : ParseUtil.parseInt(item.getItem().getMaxValue()));
        cameraView.setDatas((ArrayList<InspectTemplate_Item>) items);
        if (position == 0) {
            if (!TextUtils.isEmpty(item.getItemGroup())) {
                tv_group.setVisibility(View.VISIBLE);
                holder.setVisible(R.id.v_group, true);
                tv_group.setText(item.getItemGroup());
            } else {
                tv_group.setVisibility(View.GONE);
                holder.setVisible(R.id.v_group, false);
            }
        } else {
            if (!TextUtils.isEmpty(item.getItemGroup()) && !item.getItemGroup().equals(items.get(position - 1).getItemGroup())) {
                tv_group.setVisibility(View.VISIBLE);
                holder.setVisible(R.id.v_group, true);
                tv_group.setText(item.getItemGroup());
            } else {
                tv_group.setVisibility(View.GONE);
                holder.setVisible(R.id.v_group, false);
            }
        }
        holder.setText(R.id.tv_question, Html.fromHtml((position + 1) + ". " +
                item.getItem().getName() +
                (!TextUtils.isEmpty(item.getItem().getUnit()) ? "(" + item.getItem().getUnit() + ")" : "") +
                (item.getIsMustFill() == 1 ? " <font  color=\"#FF0000\">*</font>" : "") +
                (!TextUtils.isEmpty(item.getItem().getSubTitle()) ? "<br/> <font color=\"#8D8D8D\" ><small>" + item.getItem().getSubTitle() + "</small></font>" : "")
        ));
        final LinearLayout ll = holder.getView(R.id.ll_answer);
        //呈现
        PictureUtils.Guid2Picture(item.getFillName(), new IPictureView<ArrayList<Picture>>() {
            @Override
            public void onReceive(final ArrayList<Picture> pictures) {
                cameraView.setPosition(position);
                cameraView.setPictures(pictures);
                cameraView.setOnPictureDelete(new CameraView.OnItemDeleteListener() {
                    @Override
                    public void OnItemDelete(int integer) {
                        /**
                         * ..shizw..
                         * 图片删除之后适配器中的数据其实已经刷新了
                         * cameraView.getImageAdapter().getDatas()
                         * 就是删除图片后的新数据
                         * 但是item中的FillName和FillValue并没有更新
                         * 所以刷新后会有张空白的图片
                         * 这里把适配器中的新的数据更新过去即可
                         */
                        item.setFillName(PictureUtils.Picture2Guid((ArrayList<Picture>) cameraView.getImageAdapter().getDatas()));
                        item.setFillValue(item.getFillName());
                    }
                });
            }
        });

    }

    private void setBackground(InspectTemplate_Item item, LinearLayout ll) {
        ll.setBackgroundResource(item.getFillName().length() == 0 && item.getIsMustFill() == 1 ? R.drawable.bg_ll_frame : R.drawable.bg_ll_frame);
    }
}
